/**
 * 预处理树形结构
 * @param data 树结构字段
 * @param childrenFields
 * @param sourceFields 每一层想要转变的二维数组字段列表，比如[[id,groupName],[id,appName]]
 * @param targetFields 每一层想要转变后的对象字段列表，该数组为1维数组，比如[key,value]
 * @param height 默认传一个0即可
 * 比如：
 * const res = preDealTreeData(appOptions, ['children','children','children'],[['value','label'],['value','label'],['value','label']],['key','val'], 0)
 */
const preDealTreeData = (data,childrenFields,sourceFields,targetFields,height) => {
    if (!data || !data.length) return undefined
    return data.map(item => {
        const obj = {}
        const sourceFieldList = sourceFields[height]
        for (let i = 0;i < sourceFieldList.length;i++) {
            obj[targetFields[i]] = item[sourceFieldList[i]]
        }
        if (item[childrenFields[height]]) {
            obj['children'] = preDealTreeData(item[childrenFields[height]], childrenFields,sourceFields,targetFields,height+1)
        }
        return obj
    })
}

export default preDealTreeData